/*
 * @Author: intellectual-seeker 3336339701@qq.com
 * @Date: 2025-08-17 12:22:45
 * @LastEditors: intellectual-seeker 3336339701@qq.com
 * @LastEditTime: 2025-08-18 17:16:55
 * @FilePath: /code/lesson26/Comm.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: intellectual-seeker 3336339701@qq.com
 * @Date: 2025-08-17 12:22:45
 * @LastEditors: intellectual-seeker 3336339701@qq.com
 * @LastEditTime: 2025-08-17 22:25:04
 * @FilePath: /code/lesson26/Comm.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef COMM_HPP
#define COMM_HPP//防止头文件重复包含
#include <sys/types.h>
#include <sys/ipc.h>
#include <iostream>
using namespace std;
#include <string.h>
#include <errno.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdlib.h>

const char* filename = "/home/zyc/linux-learning";//路径要存在，绝对/相对路径皆可
int proj_id = 066;//八进制

const int default_size = 4096;//4KB

//打印错误信息时，都是cerr
//出现严重错误时，直接退出
//传错参数，返回错参数

//十进制数字转十六进制字符串
string ToHex(key_t k)//原参数：key
{
    char ret[1024];//字符数组不能用字符初始化
    snprintf(ret, sizeof(ret), "0x%x", k);//16进制格式化占位符：%x

    return ret;
}

//创建key值
key_t CreateShmKeyOrDie()//client server不传参
{
    key_t key = ftok(filename, proj_id);
    //cout << key << endl; //是负数但不为-1
    if(key == -1)//创建成功，但是退出了
    {
        cerr << "ftok error , errno : " << errno << " string errno : " << strerror(errno) << endl;
        exit(1);
    }

    return key;
}

//创建/获取
int CreateShmOrDie(key_t key, size_t size, int shmflg)
{
    int shmid = shmget(key, size, shmflg);
    if(shmid < 0)
    {
        cerr << "shmget error , errno : " << errno << " string errno : " << strerror(errno) << endl;
        exit(2);
    }

    return shmid;
}

//共享内存创建
int CreateShm(key_t key, size_t size)
{
    int shmid = CreateShmOrDie(key, size, IPC_CREAT | IPC_EXCL | 0666);//传入默认权限码

    return shmid;
}

//共享内存获取
int GetShm(key_t key, size_t size)
{
    int shmid = CreateShmOrDie(key, size, IPC_CREAT);//不存在就创建，存在就返回

    return shmid;
}

//共享内存挂载
void* ShmAttach(int shmid)
{
    void* addr = shmat(shmid, nullptr, 0);
    if((long)addr < 0)//64位系统强转为long
    {
        cerr << "shmat error , errno : " << errno << " string errno : " << strerror(errno) << endl;
    }

    return addr;
}

//共享内存解绑
void ShmDettach(const void* shmaddr)
{
    int ret = shmdt(shmaddr);
    if(ret < 0)
    {
        cerr << "shmdt error , errno : " << errno << " string errno : " << strerror(errno) << endl;
    }
}

//共享内存删除
void DeleteShm(int shmid)
{
    int ret = shmctl(shmid, IPC_RMID, nullptr);
    if(ret < 0)
    {
        cerr << "delete shm error , errno : " << errno << " string errno : " << strerror(errno) << endl;
    }
    else//删除成功打印提示信息
    {
        cout << "delete shm success!!!" << endl;
    }
}

//打印共享内存属性信息
void ShmDebug(int shmid)
{
    struct shmid_ds ds;
    int ret = shmctl(shmid, IPC_STAT, &ds);
    if(ret < 0)
    {
        cerr << "shm debug error , errno : " << errno << " string errno : " << strerror(errno) << endl;
    } 

    cout << "segsz : " << ds.shm_segsz << endl;     //大小
    cout << "nattch : " << ds.shm_nattch << endl;   //关联进程数
    cout << "ctime : " << ds.shm_ctime << endl;     //创建时间
    cout << "key : " << ds.shm_perm.__key << endl;  //唯一标识符key值
}
#endif